Skip to content

Conversation

@PCloud63514
Copy link

@PCloud63514 PCloud63514 commented Aug 20, 2025

Summary

Correct invalid examples in require package doc comments.
require does not return a boolean value; its responsibility is to fail immediately.

Changes

Updated examples to reflect proper require usage without if.
Added a new helper requireCommentParseIf to sanitize and transform invalid doc comments in bulk, ensuring consistency across the package.

Removed conditional forms such as:

if require.NoError(t, err) {
    // ...
}

Updated examples to demonstrate proper usage of require:

// NoError asserts that a function returned no error (i.e. `nil`).
//
//	  actualObj, err := SomeFunction()
//	  require.NoError(t, err)
//	  require.Equal(t, expectedObj, actualObj)
func NoError(t TestingT, err error, msgAndArgs ...interface{})

Motivation

Unlike assert, require functions do not return a boolean value.
The previous examples suggested patterns such as if require.NoError(t, err) { ... }, which are invalid.
This PR fixes those examples and introduces an automated parser (requireCommentParseIf) to prevent such mistakes from persisting in the future.

Related issues

Closes #1776

return strings.Replace(f.Comment(), search, replace, -1)
}

func requireCommentParseIf(s string) string {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method should be tested

Copy link
Collaborator

@dolmen dolmen Aug 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is codegen. Tests are not strictly necessary: checking the generated output is enough.

However, tests would still be helpful to document what the function expects and does. Tests will also be useful for future refactorings.

Even more helpful would be some godoc for the function because the require prefix is misleading: on a first read I got it as verb while it is about the require package.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An issue is that it's unclear from the function name or the code; what is this function is supposed to do. A test or a good comment before the function would help.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was my point, indeed

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#1780 (comment)
I wasn’t familiar with the reply feature, so I didn’t realize multiple conversations were happening. 😅
Considering tests, would it be better to separate this method and handle it individually?

Copy link
Collaborator

@dolmen dolmen Sep 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PCloud63514 Please start by adding a comment block to document the function.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a doc comment for requireCommentParseIf to clarify its purpose.requireCommentParseIf

Comment on lines 308 to 355
for _, line := range lines {
trim := strings.TrimSpace(line)

if !inIf {
slash := strings.Index(line, "//")
if slash < 0 {
out = append(out, line)
continue
}

trimmed := strings.TrimSpace(line[slash+2:])

if strings.HasPrefix(trimmed, "if require.") && strings.HasSuffix(trimmed, "{") {
commentPrefix = line[:slash] + "//\t "

h := strings.TrimPrefix(trimmed, "if ")
h = strings.TrimSpace(h)
if strings.HasSuffix(h, "{") {
h = strings.TrimSuffix(h, "{")
}
h = strings.TrimSpace(h)

out = append(out, commentPrefix+strings.TrimLeft(h, "\t"))
inIf = true
continue
}
out = append(out, line)
} else {
if strings.HasPrefix(trim, "//") {
body := strings.TrimSpace(strings.TrimPrefix(trim, "//"))
if body == "}" {
inIf = false
continue
}
}

slash := strings.Index(line, "//")
if slash >= 0 {
body := strings.TrimLeft(line[slash+2:], " \t")
out = append(out, commentPrefix+body)
} else {
out = append(out, line)
}
}
}

return strings.Join(out, "\n")
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm worried about a badly indented comment.

I'm unsure how to handle this correctly.

@PCloud63514
Copy link
Author

@ccoVeille Thanks, I also felt the code was a bit messy, so I tried to simplify it further.

Regarding your comment about tests: since this is only used for code generation,
I'm not sure if it makes sense to add dedicated tests here.
Do you have any suggestion for a good approach?

_codegen/main.go Outdated
commentPrefix := rePrefix.FindString(line)
comment := strings.TrimSpace(line[2:])

if strings.HasSuffix(comment, "SomeFunction()") {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like the explicit reference to SomeFunction(). This should instead look for the first line of an example block by looking at indent. But maybe there is a reason why this wasn't enough?

Copy link
Author

@PCloud63514 PCloud63514 Aug 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @dolmen

I’ve removed the explicit SomeFunction() reference.
That logic was originally added considering the type S struct { comment,
but now the handling is simplified to focus only on if require. cases.

@dolmen dolmen changed the title fix(require): correct invalid examples in doc comments #issue-1776 require: fix invalid examples in doc comments (codegen) Sep 2, 2025
@dolmen dolmen added internal/codegen Change related to internal code generation documentation pkg-require Change related to package testify/require labels Sep 2, 2025
@PCloud63514
Copy link
Author

Is there anything more I should do, or anything I might be misunderstanding?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation internal/codegen Change related to internal code generation pkg-require Change related to package testify/require

Projects

None yet

Development

Successfully merging this pull request may close these issues.

require: invalid examples from doc comments as functions don't return bool

4 participants